<template>
  <view class="new-or-edit-modal">
    <up-popup v-model:show="show" overlay mode="bottom" touchable maxHeight="'80%'" closeable closeOnClickOverlay
      @close="close" @open="open">
      <view class="modal-content">
        <view class="modal-header">
          <view class="modal-title">{{ title }}</view>
        </view>
        <up-form labelPosition="left" :model="formModel" :rules="rules" ref="formRef">
          <up-form-item labelWidth="100" label="设备编号" prop="deviceNo">
            <up-input v-model="formModel.deviceNo" placeholder="请输入设备编号" />
          </up-form-item>
          <up-form-item labelWidth="100" label="所属运营商" prop="operatorId">
            <up-select v-model:current="formModel.operatorId" :options="operatorOptions" label="请选择所属运营商">
            </up-select>
          </up-form-item>
          <up-form-item labelWidth="100" label="设备类型" prop="deviceTypeId">
            <up-select v-model:current="formModel.deviceTypeId" :options="deviceTypeOptions" label="请选择设备类型">
            </up-select>
          </up-form-item>
          <up-form-item labelWidth="100" label="视频取流方式" prop="streamTypeId">
            <up-select v-model:current="formModel.streamTypeId" :options="streamTypeOptions" label="请选择视频取流方式">
            </up-select>
          </up-form-item>
          <up-form-item labelWidth="100" label="登录IP" prop="loginIp">
            <up-input v-model="formModel.loginIp" placeholder="请输入登录IP" />
          </up-form-item>
          <up-form-item labelWidth="100" label="登录账号" prop="loginAccount">
            <up-input v-model="formModel.loginAccount" placeholder="请输入登录账号" />
          </up-form-item>
          <up-form-item labelWidth="100" label="登录密码" prop="loginPassword">
            <up-input v-model="formModel.loginPassword" placeholder="请输入登录密码" />
          </up-form-item>
          <up-form-item labelWidth="100" label="访问协议" prop="protocolId">
            <up-select v-model:current="formModel.protocolId" :options="protocolOptions" label="请选择访问协议">
            </up-select>
          </up-form-item>
        </up-form>
      </view>

      <view class="modal-footer">
        <up-button type="primary" @click="submit">确认</up-button>
      </view>
    </up-popup>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
const formRef = ref(null);
const props = defineProps({
  type: {
    type: String,
    default: 'add',
  },
  rowData: {
    type: Object,
    default: () => { },
  }
})
defineOptions({ name: "PlateRecogniModal" });

// 创建响应式数据  
const show = defineModel<boolean>("show", { default: false });
const operatorOptions = ref([
  {
    id: '0',
    name: '大良镇',
  },
  {
    id: '1',
    name: '顺德佛山交通大队',
  },
  {
    id: '2',
    name: '容桂镇',
  },
  {
    id: '3',
    name: '杏坛镇',
  },
  {
    id: '4',
    name: '伦教街道二期',
  },
  {
    id: '5',
    name: '勒流镇',
  },
])
// 'A型车牌识别(ZS-R*)', 'B型车牌识别(ZS-RF)', 'C型车牌识别(HX)', 'D型车牌识别(HIK)', 'E型车牌识别(DQ)'
const deviceTypeOptions = ref([
  {
    id: '0',
    name: 'A型车牌识别(ZS-R*)',
  },
  {
    id: '1',
    name: 'B型车牌识别(ZS-RF)',
  },
  {
    id: '2',
    name: 'C型车牌识别(HX)',
  },
  {
    id: '3',
    name: 'D型车牌识别(HIK)',
  },
  {
    id: '4',
    name: 'E型车牌识别(DQ)',
  },
])
const streamTypeOptions = ref([
  {
    id: '0',
    name: 'FLV',
  },
  {
    id: '1',
    name: 'MJPEG',
  },
])
const protocolOptions = ref([
  {
    id: '0',
    name: 'HTTP',
  },
  {
    id: '1',
    name: 'MQTT',
  },
  {
    id: '2',
    name: 'SOCKET',
  },
])
const formModel = ref(createDefaultModel());
/**
 * 初始化表单数据
 * 设备编号
 * 所属运营商下拉选择
 * 设备类型下拉选择
 * 视频取流方式下拉选择
 * 登录IP
 * 登录账号
 * 登录密码
 * 访问协议下拉选择
 */
function createDefaultModel() {
  return {
    deviceNo: '',
    operatorId: '',
    deviceTypeId: '',
    streamTypeId: '',
    loginIp: '',
    loginAccount: '',
    loginPassword: '',
    protocolId: '',
  };
}
const rules = reactive({
  deviceNo: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
  operatorId: [{ required: true, message: '请选择所属运营商', trigger: 'blur' }],
  deviceTypeId: [{ required: true, message: '请选择设备类型', trigger: 'blur' }],
  streamTypeId: [{ required: true, message: '请选择视频取流方式', trigger: 'blur' }],
  loginIp: [{ required: true, message: '请输入登录IP', trigger: 'blur' }],
  loginAccount: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
  loginPassword: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
  protocolId: [{ required: true, message: '请选择访问协议', trigger: 'blur' }],
})
function handleInitModel() {
  formModel.value = createDefaultModel();
  if (!props.rowData) return;
  if (props.type === "edit") {
    Object.assign(formModel.value, props.rowData);
  }
}
const title = computed(() => {
  const titles: any = {
    add: "新增道闸",
    edit: "编辑道闸",
  };
  return titles[props.type];
});


const submit = async () => {
  console.log(formModel.value);
  await formRef.value.validate();
}

// 定义方法  
function open() {
  show.value = true;
  handleInitModel();
  formRef.value.resetFields();
}

function close() {
  // 关闭逻辑，设置 show 为 false  
  show.value = false;
  console.log('close');
}  
</script>

<style scoped lang="scss">
.new-or-edit-modal {
  .modal-header {
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }

  .modal-content {
    padding: 20rpx;
  }
}
</style>
